package de.java.hsrm.view;

import de.java.hsrm.factory.*;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.text.ParseException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.synth.SynthLookAndFeel;

import de.java.hsrm.app.DataManager;
import de.java.hsrm.view.bars.Menubar;
import de.java.hsrm.view.panels.MainPanel;


@SuppressWarnings("serial")
public class Frame extends JFrame{
	
	private static int WIDTH = 700,HEIGHT = 400;
	
	private Dimension frameSize;
	private DataManager dataManager;
	
	private Menubar menuPanel;
	private MainPanel mp;
	
	public Frame(DataManager dm){
		this.dataManager = dm;
		initialize();
		build();
		configurate();
	}

	private void initialize() {
		this.mp = new MainPanel(this.dataManager);
		this.frameSize = new Dimension(WIDTH,HEIGHT);
		this.menuPanel = new Menubar(this.dataManager);
		new PopMenu();
		
	}

	private void build() {
		
		JPanel pane = new JPanel();
        pane.setLayout(new StackLayout());
        
        pane.add(new AvatarChooser(), StackLayout.TOP);
        
        add(pane,BorderLayout.SOUTH);
        
		add(this.mp,BorderLayout.NORTH);
		pack();
		
	}
	
	private void configurate() {
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(this.frameSize);
		setJMenuBar(this.menuPanel.getMenuBar());
		setVisible(true);
	
		initLookAndFeel();
	}
	
	private void initLookAndFeel(){
		SynthLookAndFeel laf = new SynthLookAndFeel();
		try {
			laf.load(new File("style.xml").toURL());
			UIManager.setLookAndFeel(laf);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}
//		try {
//			UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
//		} catch (ClassNotFoundException e) {
//			e.printStackTrace();
//		} catch (InstantiationException e) {
//			e.printStackTrace();
//		} catch (IllegalAccessException e) {
//			e.printStackTrace();
//		} catch (UnsupportedLookAndFeelException e) {
//			e.printStackTrace();
//		}
		SwingUtilities.updateComponentTreeUI(this);
	}
	
	public MainPanel getMp() {
		return mp;
	}

	public Menubar getMenuPanel() {
		return menuPanel;
	}
}
